package com.music.action.hbox;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.procedure.ParameterMisuseException;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.common.ParamUtil;
import com.music.domain.Music;
import com.music.domain.MusicList;
import com.music.hbox.service.FindDescMusicService;
import com.music.hbox.service.FindNewMusicService;
import com.music.hbox.service.HboxService;
import com.music.hbox.service.RankMusicService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 发现音乐相关action
 * @author LFSenior
 *
 */
@SuppressWarnings("serial")
public class HboxAction extends BaseAction{
	@Autowired
	FindNewMusicService findNewMusicService;
	
	@Autowired
	FindDescMusicService findDescMusicService;
	
	@Autowired
	RankMusicService rankMusicService;
	
	@Autowired
	HboxService hboxService;
	/**
	 * 跳转到发现音乐页面
	 * @return
	 */
	@Action(value="hbox",results={@Result(name="hbox",location="hbox.jsp")})
	public String redicterHbox(){
		request.put(BaseKey.NEW_MUSIC,findNewMusic());
		request.put(BaseKey.DESC_MUSIC, findDescMusic());
		request.put(BaseKey.RANK_MUSIC, rankMusic());
		return "hbox";
	}
	
	
	/**
	 * 查询所有新音乐
	 * @return
	 */
	public List<Music> findNewMusic(){
		return findNewMusicService.findNewMusic();
	}
	
	/**
	 * 查询推荐的音乐
	 * @return
	 */
	public List<Music> findDescMusic(){
		return findDescMusicService.findDescMusic();
	}
	
	/**
	 * 查询排行榜
	 * @return
	 */
	public List<Music> rankMusic(){
		return rankMusicService.rankMusic();
	}
	
	
	/**
	 * 收藏音乐
	 * @return
	 */
	@Action(value="addMusic",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String addMusic(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			hboxService.addMusic(ParamUtil.buildUserId(ServletActionContext.getRequest()), param.get("musicid").toString());
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
	
	/**
	 * 查询用户收藏的音乐
	 * @return
	 */
	@Action(value="findAllUserMusic",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String findAllUserMusic(){
		try {
			List<MusicList> data=hboxService.findAllUserMusic(ParamUtil.buildUserId(ServletActionContext.getRequest()));
			reParam.put("data", data);
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
	
	/**
	 * 删除用户的相关
	 * @return
	 */
	@Action(value="deleteUserMusic",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String deleteUserMusic(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			hboxService.deleteUserMusic(ParamUtil.buildUserId(ServletActionContext.getRequest()),param.get("musicid").toString());
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
}
